package spectrum;
import ikelib.menuUtil;

import ikelib.ChannelData;
import ikelib.menuUtil;
import ikelib.wintime;

import java.awt.GridBagLayout;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.ListModel;

import java.awt.BorderLayout;
import javax.swing.JLabel;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.ScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.BoxLayout;

import wcg.ChannelDataCollection;
import wcg.winDataBase;
import wcg.winDataBaseChange;
import wcg.winchkgPanelInterface;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;





public class SpectrumViewPanel extends JPanel implements MenuListener, ActionListener, winchkgPanelInterface ,ItemListener {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelSpectrumControl = null;  //  @jve:decl-index=0:visual-constraint="10,127"
	private JLabel jLabelTitle = null;
	private SpectrumView jPanelSpectrumView = null;  //  @jve:decl-index=0:visual-constraint="27,767"
	private winDataBase Data=null;  //  @jve:decl-index=0:
	private JList jListChannelSelect = null;
	private JScrollPane jScrollPane1 = null;
	private JLabel jLabelamp = null;
	private JButton jButtonRefLevelUp = null;
	private JButton jButtonRefLevelDown = null;
	private JButton jButtonSpanUp = null;
	private JButton jButtonSpanDown = null;
	private JLabel jLabelamp2 = null;
	private JButton jButtonMaxUp = null;
	private JButton jButtonMaxDown = null;
	private JLabel jLabelamp21 = null;
	private JCheckBox jCheckBoxXLogScale = null;
	private JComboBox jComboBoxWindow = null;
	private JComboBox jComboBoxFFTLength = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JCheckBox jCheckBoxOverLapping = null;
	private JTextArea jTextAreaInputInfo = null;
	private String InfoInput  = "Input info";
	private String InfoFFT  = "FFT info";
	
	private JLabel jLabelampFreqMin = null;
	private JButton jButtonMinDown = null;
	private JButton jButtonMinUp = null;
	private JCheckBox jCheckBoxDCOffsetCancel = null;
	private JCheckBox jCheckBoxAutoUpDate = null;
	private boolean AutoUpdate = false;
	private int AutoUpdateDataLength = 4096;
	private int FFTDataLength = 4096;
	//private int LastTime = 0;  //  @jve:decl-index=0:
	private int DiapChannel;
	private int SampleRate=1;
	private JComboBox jComboUpdatePeriod = null;
	private int FFTCount=0;
	private Date LastDate = new Date(0);  //  @jve:decl-index=0:
	private JButton jButtonAutoLevel = null;
	private JButton jButtonSnapShot = null;
	private wintime DataTimeStart = new wintime();  //  @jve:decl-index=0:
	private wintime DataTimeEnd   = new wintime();  //  @jve:decl-index=0:
	private JComboBox jComboBoxMaxDataLength = null;
	private int MaxDataLength = 1024*256;
	private JLabel jLabel3 = null;
	private JButton jButtonReCalc = null;
	private JComboBox jComboBoxCalcMode = null;
	private JLabel jLabel4 = null;
	private JCheckBox jCheckBoxRelative = null;
	
	private JCheckBox chckbxDensity=null;

	
	
	private JRadioButtonMenuItem ColorColorSpectrumWhite = null;
	private JRadioButtonMenuItem ColorColorSpectrumBlack = null;
	private JRadioButtonMenuItem ColorBWSpectrumWhite = null;
	private JRadioButtonMenuItem ColorBWSpectrumBlack = null;

	
	private JRadioButtonMenuItem ColorSpectrumMapByChannel  = null;
	private JRadioButtonMenuItem ColorSpectrumMapByStation  = null;
	private JRadioButtonMenuItem ColorSpectrumMapByComponent= null;

	
	
	
	// ------------------------------------------
	public SpectrumView getSpectrumView(){
		if(jPanelSpectrumView==null){
			jPanelSpectrumView = new SpectrumView(); // @
			//jPanelSpectrumView.ParentPanel=this;
		}
		return jPanelSpectrumView;
	}
	// ------------------------------------------
	public void setwinDataBase(winDataBase d){
		Data=d;
		jPanelSpectrumView.setWinDataBase(d);
	}
	public winDataBase getwinDataBase(){
		return Data;
	}
	/**
	 * This is the default constructor
	 */
	public SpectrumViewPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(500, 750);
		this.setLayout(new BorderLayout());
		this.add(getJPanelSpectrumView(), BorderLayout.CENTER);
		this.add(getJPanelBottomPanel(), BorderLayout.SOUTH);
		this.add(getJPanelSpectrumControl(), BorderLayout.WEST);
	}

	/**
	 * This method initializes jPanelSpectrumControl	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelSpectrumControl() {
		if (jPanelSpectrumControl == null) {
			jLabelLogo = new JLabel();
			jLabelLogo.setBounds(new Rectangle(10, 4, 180, 32));
			jLabelLogo.setText("");
			jLabelLogo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/hakusan_180_32.png")));
			jPanelSpectrumControl = new JPanel();
			jPanelSpectrumControl.setLayout(null);
			jPanelSpectrumControl.setPreferredSize(new Dimension(206,700));
			jPanelSpectrumControl.setSize(new Dimension(206, 760));  // Generated
			jPanelSpectrumControl.add(getJLabelTitle(), BorderLayout.WEST);
			jPanelSpectrumControl.add(jLabelLogo, null);
			
			JPanel panelAmp = new JPanel();
			panelAmp.setBorder(new TitledBorder(null, "Amplitude", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panelAmp.setBounds(0, 157, 200, 122);
			jPanelSpectrumControl.add(panelAmp);
			panelAmp.setLayout(null);
			jLabelamp = new JLabel();
			jLabelamp.setBounds(13, 23, 64, 21);
			panelAmp.add(jLabelamp);
			jLabelamp.setText("Ref. Level");
			panelAmp.add(getJButtonRefLevelDown());
			panelAmp.add(getJButtonRefLevelUp());
			jLabelamp2 = new JLabel();
			jLabelamp2.setBounds(107, 23, 41, 20);
			panelAmp.add(jLabelamp2);
			jLabelamp2.setText("Amp.");
			panelAmp.add(getJButtonSpanDown());
			panelAmp.add(getJButtonSpanUp());
			panelAmp.add(getJCheckBoxRelative());
			
			chckbxDensity = new JCheckBox("Density");
			chckbxDensity.setBounds(12, 86, 85, 24);
			panelAmp.add(chckbxDensity);
			
			JPanel panelFreq = new JPanel();
			panelFreq.setBorder(new TitledBorder(null, "Frequency", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panelFreq.setBounds(0, 279, 200, 83);
			jPanelSpectrumControl.add(panelFreq);
			panelFreq.setLayout(null);
			panelFreq.add(getJButtonMaxUp());
			panelFreq.add(getJButtonMaxDown());
			jLabelamp21 = new JLabel();
			jLabelamp21.setBounds(157, 27, 33, 23);
			panelFreq.add(jLabelamp21);
			jLabelamp21.setText("Max");
			panelFreq.add(getJCheckBoxXLogScale());
			jLabelampFreqMin = new JLabel();
			jLabelampFreqMin.setBounds(16, 25, 30, 25);
			panelFreq.add(jLabelampFreqMin);
			jLabelampFreqMin.setText("Min");
			panelFreq.add(getJButtonMinDown());
			panelFreq.add(getJButtonMinUp());
			panelFreq.add(getJCheckBoxXPeriod());
			
			JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(null, "Calculate and Display", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel.setBounds(0, 363, 200, 185);
			jPanelSpectrumControl.add(panel);
			panel.setLayout(null);
			
						jLabel4 = new JLabel();
						jLabel4.setBounds(13, 22, 47, 23);
						panel.add(jLabel4);
						jLabel4.setText("Mode");  // Generated
						panel.add(getJComboBoxCalcMode());
						jLabel = new JLabel();
						jLabel.setBounds(12, 50, 70, 22);
						panel.add(jLabel);
						jLabel.setText("window");
						panel.add(getJComboBoxWindow());
						jLabel1 = new JLabel();
						jLabel1.setBounds(11, 75, 88, 22);
						panel.add(jLabel1);
						jLabel1.setText("FFT Length");
						panel.add(getJComboBoxFFTLength());
						panel.add(getJCheckBoxOverLapping());
						jLabel3 = new JLabel();
						jLabel3.setBounds(13, 124, 106, 22);
						panel.add(jLabel3);
						jLabel3.setText("Max Data Length");
						panel.add(getJComboBoxMaxDataLength());
						panel.add(getJCheckBoxDCOffsetCancel());
						jPanelSpectrumControl.add(getJScrollPane2());
						jPanelSpectrumControl.add(getPanel_1());
			chckbxDensity.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {
					if(chckbxDensity.isSelected()){
						jPanelSpectrumView.setSpectrumDensity(true);
					}
					else{
						jPanelSpectrumView.setSpectrumDensity(false);
					}
				}
			});
			
		}
		return jPanelSpectrumControl;
	}

	/**
	 * This method initializes jLabelTitle	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JLabel getJLabelTitle() {
		if (jLabelTitle == null) {
			jLabelTitle = new JLabel();
			jLabelTitle.setText("Spectrum View");
		}
		return jLabelTitle;
	}

	/**
	 * This method initializes jPanelSpectrumView	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelSpectrumView() {
		if (jPanelSpectrumView == null) {
			jPanelSpectrumView = new SpectrumView();
			jPanelSpectrumView.parent = this;
			jPanelSpectrumView.setLayout(new GridBagLayout());
		}
		return jPanelSpectrumView;
	}
	
	/**
	 * This method initializes jListChannelSelect	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJListChannelSelect() {
		if (jListChannelSelect == null) {
			jListChannelSelect = new JList();
			jListChannelSelect.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
				public void valueChanged(javax.swing.event.ListSelectionEvent e) {
					//jPanelSpectrumView.setAutoScale();
					if(!e.getValueIsAdjusting()){
						jPanelSpectrumView.ChannelDataReLoad();
						doFFT(0);
					}
				}
			});
		}
		return jListChannelSelect;
	}
	
	public void doGetChannel(){
		jListChannelSelect.removeAll();
		DefaultListModel model = new DefaultListModel(); //@
		//Iterator<Integer> ite = Data.getChannel().iterator();
		Iterator<Integer> ite = Data.iteratorChannel();
		while(ite.hasNext()){
			int ch = ite.next();
			String channel = new String();
			channel=String.format("%01X%01X%01X%01X", (ch>>12)&0xf,(ch>>8)&0xf,(ch>>4)&0xf,(ch>>0)&0xf);
			model.addElement(channel);
		}
		jListChannelSelect.setModel(model);
	}
	private int GetSelectedChannel(){
		ListModel model= jListChannelSelect.getModel(); //@
		int index=jListChannelSelect.getSelectedIndex();
		if(index<0) return -1;
		String chtxt = (String)model.getElementAt(index);
		
		int ret = 0;
		try{
			ret = Integer.parseInt(chtxt,16);
		} catch(NumberFormatException e){
			e.printStackTrace();
		}
		return ret;
	}
	private int[] getSelectedChannels(){
		ListModel model= jListChannelSelect.getModel(); //@
		int[] chtxts =jListChannelSelect.getSelectedIndices();
		if(chtxts.length <= 0) return null;
		
		int[] ret = new int[chtxts.length];
		for(int i=0;i<chtxts.length;i++){
			try{
				String ch = model.getElementAt(chtxts[i]).toString();
				ret[i] = Integer.parseInt(ch,16);
			} catch(NumberFormatException e){
				e.printStackTrace();
			}
		}
		return ret;
	}
	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(6, 23, 70, 82);
			jScrollPane1.setViewportView(getJListChannelSelect());
		}
		return jScrollPane1;
	}
	/**
	 * This method initializes jButtonRefLevelUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonRefLevelUp() {
		if (jButtonRefLevelUp == null) {
			jButtonRefLevelUp = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-down.png")));
			jButtonRefLevelUp.setBounds(79, 45, 25, 25);
			//jButtonRefLevelUp.setText("UP");
			jButtonRefLevelUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanelSpectrumView.onYLevelUp();
				}
			});
		}
		return jButtonRefLevelUp;
	}
	/**
	 * This method initializes jButtonRefLevelDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonRefLevelDown() {
		if (jButtonRefLevelDown == null) {
			jButtonRefLevelDown = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-up.png")));
			jButtonRefLevelDown.setBounds(79, 21, 25, 25);
			//jButtonRefLevelDown.setText("DN");
			jButtonRefLevelDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanelSpectrumView.onYLevelDown();
				}
			});
		}
		return jButtonRefLevelDown;
	}
	/**
	 * This method initializes jButtonSpanUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSpanUp() {
		if (jButtonSpanUp == null) {
			jButtonSpanUp = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-shrink.png")));
			jButtonSpanUp.setBounds(159, 45, 25, 25);
			//jButtonSpanUp.setText("UP");
			jButtonSpanUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanelSpectrumView.onYSpanUp();
				}
			});
		}
		return jButtonSpanUp;
	}
	/**
	 * This method initializes jButtonSpanDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSpanDown() {
		if (jButtonSpanDown == null) {
			jButtonSpanDown = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-expand.png")));
			jButtonSpanDown.setBounds(159, 21, 25, 25);
			//jButtonSpanDown.setText("DN");
			jButtonSpanDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanelSpectrumView.onYSpanDown();
				}
			});
		}
		return jButtonSpanDown;
	}
	public void message(String mes){
		//jTextAreaInfo.setText(jTextAreaInfo.getText()+mes);
	}
	/**
	 * データの開始時刻を返す
	 * @return
	 */
	public wintime getDataTimeStart()
	{
		wintime ret = new wintime(DataTimeStart);
		return ret;
	}
	/**
	 * データの終了時刻を返す
	 * @return
	 */
	public wintime getDataTimeEnd()
	{
		wintime ret = new wintime(DataTimeEnd);
		return ret;
	}
	// ------------------------------------------
	// スペクトラムビューの変化
	public void onSpectrumViewChange(){
		String mes = new String();
		int datalen = jPanelSpectrumView.getDataLength();
		int fftlen = jPanelSpectrumView.getFFTlength();
		mes = String.format("CH=%04x FFT calc#=%d\n%s\n%s\nData length=%,d\nSample rate=%d" , 
				DiapChannel,FFTCount,DataTimeStart.fmt(),DataTimeEnd.fmt(),datalen,
				jPanelSpectrumView.getFFTSamplerate());
//		if(jPanelSpectrumView.getDataLength()<512){
//			mes += "Too less data!";
//		}
		//jTextAreaInputInfo.setText(mes);
		InfoInput  = mes;
		setInfo();
		
		mes = String.format("DATA/FFT=%d/%d=%.1f" ,datalen, fftlen , (double)datalen/(double)fftlen);
		if(datalen < fftlen){
			mes += "\n[Zero inserted]";
		}
		mes += String.format("\nAverage=%d\nResolution=%5.5fHz(%4.1fdB)" ,
				jPanelSpectrumView.getOverlapCount(),
				jPanelSpectrumView.getFreqResolution(),
				10*Math.log10((double)jPanelSpectrumView.getFreqResolution()));
		double fullscale = jPanelSpectrumView.getFullScall();
		mes += String.format("\nFullScale=%10.3f ", fullscale);
		
		double level = jPanelSpectrumView.getMarkerLevel(0);
		double freq = jPanelSpectrumView.getMarkerFreq(0);
		mes += String.format("\nMarker %5.2fdB %5.3fHz", level,freq);
		
		//jTextAreaFFTInfo.setText(mes);
		InfoFFT = mes;
		setInfo();
	}
	/**
	 * This method initializes jButtonMaxUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonMaxUp() {
		if (jButtonMaxUp == null) {
			jButtonMaxUp = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-max-up.png")));
			jButtonMaxUp.setBounds(126, 25, 25, 25);
			//jButtonMaxUp.setText("UP");
			jButtonMaxUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					jPanelSpectrumView.setDisplayFreqMaxUp();
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jButtonMaxUp;
	}
	/**
	 * This method initializes jButtonMaxDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonMaxDown() {
		if (jButtonMaxDown == null) {
			jButtonMaxDown = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/spectrum-max-down.png")));
			jButtonMaxDown.setBounds(104, 25, 25, 25);
			//jButtonMaxDown.setText("DN");
			jButtonMaxDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					jPanelSpectrumView.setDisplayFreqMaxDown();
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jButtonMaxDown;
	}
	/**
	 * This method initializes jCheckBoxXLogScale	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxXLogScale() {
		if (jCheckBoxXLogScale == null) {
			jCheckBoxXLogScale = new JCheckBox();
			jCheckBoxXLogScale.setBounds(16, 54, 114, 16);
			jCheckBoxXLogScale.setText("Freq. log scale");
			jCheckBoxXLogScale.setSelected(true);
			jCheckBoxXLogScale.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					if(jCheckBoxXLogScale.isSelected()){
						jPanelSpectrumView.setXLogScale(true);
						getJCheckBoxOffsetDisplay().setEnabled(true);
					}
					else{
						jPanelSpectrumView.setXLogScale(false);
					}
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jCheckBoxXLogScale;
	}
	/**
	 * This method initializes jComboBoxWindow	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxWindow() {
		if (jComboBoxWindow == null) {
			String[] comboDat = {"Rect","Hann","Hamming","Blackman"};
			DefaultComboBoxModel model = new DefaultComboBoxModel(comboDat); // @
			jComboBoxWindow = new JComboBox(); 
			jComboBoxWindow.setBounds(103, 47, 81, 25);
			jComboBoxWindow.setModel(model);
			jComboBoxWindow.setSelectedIndex(1);
			jComboBoxWindow.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
				//	System.out.println("itemStateChanged()"); // Auto-generated Event stub itemStateChanged()
					int sel = jComboBoxWindow.getSelectedIndex();
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					jPanelSpectrumView.setWindowFunction(sel);
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jComboBoxWindow;
	}
	/**
	 * This method initializes jComboBoxFFTLength	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxFFTLength() {
		if (jComboBoxFFTLength == null) {
			String[] comboDat = {"16","32","64","128","256","512","1024","2048","4096","8192","16384","32768","65536","128k","256k","512k","1M","Muximum"};
			DefaultComboBoxModel model = new DefaultComboBoxModel(comboDat); // @
			jComboBoxFFTLength = new JComboBox();
			jComboBoxFFTLength.setBounds(103, 74, 80, 25);
			jComboBoxFFTLength.setModel(model);
			jComboBoxFFTLength.setSelectedIndex(8);
			jComboBoxFFTLength.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					//System.out.println("itemStateChanged()"); //  Auto-generated Event stub itemStateChanged()
					int num = jComboBoxFFTLength.getSelectedIndex();
					if(num==17){
						num = 0;
					}
					else{
						num = (int)Math.pow(2, num+4);
					}
					FFTDataLength = num; 
					jPanelSpectrumView.setFFTlength(num);
//					switch(jComboBoxFFTLength.getSelectedIndex()){
//					case 0: FFTDataLength = 512; 	jPanelSpectrumView.setFFTlength(512); break;
//					case 1: FFTDataLength = 1024; 	jPanelSpectrumView.setFFTlength(1024); break;
//					case 2: FFTDataLength = 2048; 	jPanelSpectrumView.setFFTlength(2048); break;
//					case 3: FFTDataLength = 4096;	jPanelSpectrumView.setFFTlength(4096); break;
//					case 4: FFTDataLength = 8192;	jPanelSpectrumView.setFFTlength(8192); break;
//					case 5: FFTDataLength = 16384;	jPanelSpectrumView.setFFTlength(16384); break;
//					case 6: FFTDataLength = 32768;	jPanelSpectrumView.setFFTlength(32768); break;
//					case 7: FFTDataLength = 65536;	jPanelSpectrumView.setFFTlength(65536); break;
//					case 8: FFTDataLength = 131072;	jPanelSpectrumView.setFFTlength(131072); break;
//					case 9: FFTDataLength = 262144;	jPanelSpectrumView.setFFTlength(262144); break;
//					case 10: FFTDataLength = 524288;	jPanelSpectrumView.setFFTlength(524288); break;
//					case 11: FFTDataLength = 1048576;	jPanelSpectrumView.setFFTlength(1048576); break;
//					default: FFTDataLength = 0;		jPanelSpectrumView.setFFTlength(0); break;
//					}
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR )));
				}
			});
		}
		return jComboBoxFFTLength;
	}
	/**
	 * This method initializes jCheckBoxOverLapping	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxOverLapping() {
		if (jCheckBoxOverLapping == null) {
			jCheckBoxOverLapping = new JCheckBox();
			jCheckBoxOverLapping.setBounds(8, 103, 90, 18);
			jCheckBoxOverLapping.setText("Average");
			jCheckBoxOverLapping.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
				//	System.out.println("itemStateChanged()"); //  Auto-generated Event stub itemStateChanged()
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					if(jCheckBoxOverLapping.isSelected()){
						jPanelSpectrumView.setOverLapping(true);
						jComboBoxMaxDataLength.setEnabled(true);
					}
					else{
						jPanelSpectrumView.setOverLapping(false);
						jComboBoxMaxDataLength.setEnabled(false);
					}
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jCheckBoxOverLapping;
	}
	/**
	 * This method initializes jTextAreaInputInfo	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaInputInfo() {
		if (jTextAreaInputInfo == null) {
			jTextAreaInputInfo = new JTextArea();
		}
		return jTextAreaInputInfo;
	}
	private void setInfo(){
		String mes = "[ Input info ] \n" + InfoInput + "\n[ FFT info ]\n" + InfoFFT;
		jTextAreaInputInfo.setText(mes);
	}
	/**
	 * This method initializes jButtonMinDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonMinDown() {
		if (jButtonMinDown == null) {
			jButtonMinDown = new JButton(new ImageIcon(getClass().getClassLoader()
					.getResource("images/spectrum-min-down.png")));
			jButtonMinDown.setBounds(46, 25, 25, 25);
			jButtonMinDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					jPanelSpectrumView.setDisplayFreqMinDown();
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jButtonMinDown;
	}
	/**
	 * This method initializes jButtonMinUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonMinUp() {
		if (jButtonMinUp == null) {
			jButtonMinUp = new JButton(new ImageIcon(getClass().getClassLoader()
					.getResource("images/spectrum-min-up.png")));
			jButtonMinUp.setBounds(70, 25, 25, 25);
			jButtonMinUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					jPanelSpectrumView.setDisplayFreqMinUp();
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jButtonMinUp;
	}
	/**
	 * This method initializes jCheckBoxDCOffsetCancel	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxDCOffsetCancel() {
		if (jCheckBoxDCOffsetCancel == null) {
			jCheckBoxDCOffsetCancel = new JCheckBox();
			jCheckBoxDCOffsetCancel.setBounds(9, 148, 143, 18);
			jCheckBoxDCOffsetCancel.setText("DC offset cancel");
			jCheckBoxDCOffsetCancel.setSelected(true);
			jCheckBoxDCOffsetCancel.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					setCursor((Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
					if(jCheckBoxDCOffsetCancel.isSelected()) jPanelSpectrumView.setDCOffsetCancel(true);
					else jPanelSpectrumView.setDCOffsetCancel(false);
					setCursor((Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)));
				}
			});
		}
		return jCheckBoxDCOffsetCancel;
	}
	/**
	 * This method initializes jCheckBoxAutoUpDate	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxAutoUpDate() {
		if (jCheckBoxAutoUpDate == null) {
			jCheckBoxAutoUpDate = new JCheckBox();
			jCheckBoxAutoUpDate.setText("Auto update");
			jCheckBoxAutoUpDate.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if(jCheckBoxAutoUpDate.isSelected()){
						getJComboUpdatePeriod().setEnabled(true);
						AutoUpdate  = true;
					}
					else{
						getJComboUpdatePeriod().setEnabled(false);
						AutoUpdate = false;
					}
				}
			});
		}
		return jCheckBoxAutoUpDate;
	}
	/**
	 * This method initializes jComboUpdatePeriod	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboUpdatePeriod() {
		if (jComboUpdatePeriod == null) {
			String[] comboDat = {"1sec","3sec","10sec","30sec","1min","3min","10min","30min"};
			DefaultComboBoxModel model = new DefaultComboBoxModel(comboDat); //@
			jComboUpdatePeriod = new JComboBox();
			jComboUpdatePeriod.setPreferredSize(new Dimension(120,16));
			
			jComboUpdatePeriod.setModel(model);
			jComboUpdatePeriod.setSelectedIndex(3);
			jComboUpdatePeriod.setEnabled(false);
			jComboUpdatePeriod.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					//System.out.println("itemStateChanged()"); //  Auto-generated Event stub itemStateChanged()
					switch(jComboUpdatePeriod.getSelectedIndex()){
					case 0: AutoUpdateDataLength = 1 ; break;
					case 1: AutoUpdateDataLength = 3 ; break;
					case 2: AutoUpdateDataLength = 10 ; break;
					case 3: AutoUpdateDataLength = 30 ; break;
					case 4: AutoUpdateDataLength = 60 ; break;
					case 5: AutoUpdateDataLength = 60*3 ; break;
					case 6: AutoUpdateDataLength = 60*10 ; break;
					default: AutoUpdateDataLength = 60*30 ; break;
					}
				}
			});
		}
		return jComboUpdatePeriod;
	}
	/**
	 * This method initializes jButtonAutoLevel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAutoLevel() {
		if (jButtonAutoLevel == null) {
			jButtonAutoLevel = new JButton();
			jButtonAutoLevel.setBounds(82, 54, 98, 26);
			jButtonAutoLevel.setText("Auto Scale");
			jButtonAutoLevel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jPanelSpectrumView.setAutoScale();
				}
			});
		}
		return jButtonAutoLevel;
	}
	/**
	 * This method initializes jButtonSnapShot	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSnapShot() {
		if (jButtonSnapShot == null) {
			jButtonSnapShot = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/camera.png")));
			jButtonSnapShot.setToolTipText("Take a snapshot to .png file");
			jButtonSnapShot.setPreferredSize(new Dimension(30,30));
			jButtonSnapShot.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String dir = jPanelSpectrumView.ExportImageDir;
					if(dir == null) dir = Data.getWriteOutFolder();
					jPanelSpectrumView.exportAsImage(dir,1.0,1.0);
				}
			});
		}
		return jButtonSnapShot;
	}
	private int[] getData(int ch,int maxlength){
		// ------------------------------
		// ｃｈ-データコレクションを得て
		ChannelDataCollection dcol = Data.getChannelDataCollection(ch);
		if(dcol==null) return null;
		TreeMap<Integer,ChannelData> cdlist = dcol.getChannelData();
		
		if(maxlength==0 || jCheckBoxOverLapping.isSelected()) maxlength = MaxDataLength;
		// ------------------------------
		// ｃｈ-データ長を計算
		// 最新表示データから数えて指定数を超えるまで。。。
		int length=0;
		int key = cdlist.lastKey();
		wintime stwtdata = new wintime(key);
		wintime stwt = new wintime(Data.DispStartTimeSec);
		stwt.inc_sec((int)Data.DispSpanSec);
		if(stwtdata.getValueS() < stwt.getValueS()){
			stwt = stwtdata;
		}
		int  em = Data.DispStartTimeSec.getValueS();
		int i,tm;
		for(i=0;length<maxlength ;i++,stwt.dec_sec()){
			tm = stwt.getValueS();
			if(tm<em){
				break;
			}
			ChannelData cd  = cdlist.get(tm);
			if(cd == null) break;;
			DiapChannel=ch;
			SampleRate= cd.getSamplerate();
			length += cd.getData().length;
		}
		if(length<=0) return null;
		stwt.inc_sec();
		// ------------------------------
		// ｃｈ-データをすべてコピーして
		int max = i;
		int [] dt  = new int[length];
		int index=0;
		DataTimeStart = new wintime(stwt);
		for(i=0;i<max;i++,stwt.inc_sec()){
			tm = stwt.getValueS();
			ChannelData cd  = cdlist.get(tm);
			if(cd==null) break;
			System.arraycopy(cd.getData(), 0, dt, index, cd.getData().length);
			index += cd.getData().length;
		}
		stwt.dec_sec();
		DataTimeEnd = new wintime(stwt);
		
//		wintime sttime = new wintime(Data.DispStartTimeSec);
//		long span = Data.DispSpanSec;
//		int length=0;
//		for(int i=0;i<span;i++,sttime.inc_sec()){
//			int tm = sttime.getValueS();
//			ChannelData cd  = cdlist.get(tm);	
//			if(cd==null) continue;
//			length+=cd.getData().length;
//		}
//		if(length<=0) return null;
//		int [] dt  = new int[length];
//		sttime = new wintime(Data.DispStartTimeSec);
//		DataTimeStart = new wintime(sttime);
//		int index=0;
//		for(int i=0;i<span;i++,sttime.inc_sec()){
//			int tm = sttime.getValueS();
//			ChannelData cd  = cdlist.get(tm);	
//			if(cd==null) continue;
//			System.arraycopy(cd.getData(), 0, dt, index, cd.getData().length);
//			index += cd.getData().length;
//		}
//		sttime.dec_sec();
//		DataTimeEnd = new wintime(sttime);
		return dt;
	}
	
	private void doFFT(int maxlength){
		// ｃｈをえらんで
		int[] ch = getSelectedChannels();
		if(ch==null) return;
		jPanelSpectrumView.clearDataAll();
		for(int i=0;i<ch.length;i++){
			doFFT(ch[i],maxlength);
		}
		int cch = jPanelSpectrumView.getRefChannel();
		for(int i=0;i<ch.length;i++){
			if(cch == ch[i]) break;
		}
		jPanelSpectrumView.setRefChannel(ch[0]);
	}
	//----------------------------------------------------
	private static int[] TestData = { 5,32,38,-33,
		-19,-10,1,-8,
		-20,10,-1,4,
		11,-1,-7,-2	};
	private JCheckBox jCheckBoxXPeriod = null;
	private JScrollPane jScrollPane2 = null;
	private JCheckBox jCheckBoxOffsetDisplay = null;
	private JPanel jPanelBottomPanel = null;
	private JLabel jLabelLogo = null;
	private JLabel jLabelAmplitudeGroupe = null;
	private JLabel jLabelAmplitudeGroupe1 = null;
	private JLabel jLabelAmplitudeGroupe11 = null;
	private JPanel jPanelWest = null;
	private JPanel panel_1;
	private void doFFT(int ch,int maxlength){
		// ------------------------------
		// データを抜き出して
		int [] dt = getData(ch,maxlength);
		if(dt == null) return;
		if(dt.length<=0) return;
		if(jPanelSpectrumView.isBusy()) return;
		// テストデータ
//		dt = TestData;
//		SampleRate = 2;
//		ch = 0x1111;
		// ------------------------------
		// ｃｈ-データをセット
		//jTextAreaInputInfo.setText("");
		if(jPanelSpectrumView.setData(dt,SampleRate,ch)){
			LastDate = new Date();
			FFTCount++;
			
		}
		else{
			//jTextAreaInputInfo.setText("Error");
			InfoInput  = "Error";
			setInfo();
			if(jPanelSpectrumView.getDataLength()<16){
				//jTextAreaInputInfo.setText("Error:Too less FFT point\nZoomOut Span in Time-Chart!");
				InfoInput  = "Error:Too less FFT point\nZoomOut Span in Time-Chart!";
				setInfo();
			}
		}
	}


	//------------------------------------------------------------------------
	// 元のデータベースが変化したとき呼び出される
	private void OnChangeData(){
		if(!AutoUpdate) return;
		Date d = new Date();
		long time = d.getTime() - LastDate.getTime();
		if(time<100) {
			return;
		}
		// ------------------------------
		// ｃｈをえらんで
		int ch = GetSelectedChannel();
		if(ch<0) return;
		if(ch != DiapChannel){
			doFFT(FFTDataLength);
		}
		else{
			if(time/1000>=AutoUpdateDataLength){
				doFFT(FFTDataLength);
			}
		}
		
	}
	//------------------------------------------------------------------------
	// 元のデータベースが変化したとき呼び出される
	public void OnChangeDataBase(int hint,int ch,ChannelDataCollection dcol){
		switch(hint){
		case winDataBaseChange.EVENT_CHANNEL_CLEAR:
		case winDataBaseChange.EVENT_CHANNEL_ADD:
		case winDataBaseChange.EVENT_CHANNEL_REMOVE:
			doGetChannel();
			break;
		case winDataBaseChange.EVENT_DATA_CLEAR:
			break;
		case winDataBaseChange.EVENT_DATA_REMOVE:
			break;
		case winDataBaseChange.EVENT_DATA_PUT:
			OnChangeData();
			break;
		}
	}
	/**
	 * This method initializes jComboBoxMaxDataLength	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxMaxDataLength() {
		if (jComboBoxMaxDataLength == null) {
			String[] comboDat = {"256KB","512KB","1M","2M","4M","8M"};
			DefaultComboBoxModel model = new DefaultComboBoxModel(comboDat); //@
			jComboBoxMaxDataLength = new JComboBox();
			jComboBoxMaxDataLength.setBounds(121, 123, 73, 24);
			jComboBoxMaxDataLength.setModel(model);
			jComboBoxMaxDataLength.setSelectedIndex(0);
			jComboBoxMaxDataLength.setEnabled(false);
			jComboBoxMaxDataLength.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					switch(jComboBoxMaxDataLength.getSelectedIndex()){
					case 0: MaxDataLength = 1024*256 ; break;
					case 1: MaxDataLength = 1024*512 ; break;
					case 2: MaxDataLength = 1024*1024 ; break;
					case 3: MaxDataLength = 1024*1024*2 ; break;
					case 4: MaxDataLength = 1024*1024*4 ; break;
					case 5: MaxDataLength = 1024*1024*8 ; break;
					default: MaxDataLength = 1024*256 ; break;
					}
					doFFT(0);
				}
			});
		}
		return jComboBoxMaxDataLength;
	}
	/**
	 * This method initializes jButtonReCalc	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReCalc() {
		if (jButtonReCalc == null) {
			jButtonReCalc = new JButton();
			jButtonReCalc.setBounds(81, 23, 99, 24);
			jButtonReCalc.setText("ReCalc");  // Generated
			jButtonReCalc.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					doFFT(FFTDataLength);
				}
			});
		}
		return jButtonReCalc;
	}
	/**
	 * This method initializes jComboBoxCalcMode	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxCalcMode() {
		if (jComboBoxCalcMode == null) {
			String[] comboDat = {"Power","Fourier Amp."};
			DefaultComboBoxModel model = new DefaultComboBoxModel(comboDat); // @
			jComboBoxCalcMode = new JComboBox();
			jComboBoxCalcMode.setBounds(73, 20, 111, 24);
			jComboBoxCalcMode.setModel(model);
			jComboBoxCalcMode.setSelectedIndex(0);
			jComboBoxCalcMode.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					int sel = jComboBoxCalcMode.getSelectedIndex();
					switch(sel){
					case 0:
						jPanelSpectrumView.setMode(jPanelSpectrumView.CALC_MODE_DBFSPOWER);
						break;
					case 1:
						jPanelSpectrumView.setMode(jPanelSpectrumView.CALC_MODE_DBFSAMP);
						break;
						
					}
					
				}
			});
		}
		return jComboBoxCalcMode;
	}
	/**
	 * This method initializes jCheckBoxRelative	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxRelative() {
		if (jCheckBoxRelative == null) {
			jCheckBoxRelative = new JCheckBox();
			jCheckBoxRelative.setBounds(12, 66, 144, 21);
			jCheckBoxRelative.setText("Relative to fullscale");  // Generated
			jCheckBoxRelative.setSelected(true);
			jCheckBoxRelative.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					jPanelSpectrumView.setRelative(jCheckBoxRelative.isSelected());
					//jPanelSpectrumView.ChannelDataReLoad();
					//doFFT(0);
				}
			});
		}
		return jCheckBoxRelative;
	}
	/**
	 * This method initializes jCheckBoxXPeriod	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxXPeriod() {
		if (jCheckBoxXPeriod == null) {
			jCheckBoxXPeriod = new JCheckBox();
			jCheckBoxXPeriod.setBounds(130, 55, 64, 15);
			jCheckBoxXPeriod.setText("Period");  // Generated
			jCheckBoxXPeriod.setSelected(false);  // Generated
			jCheckBoxXPeriod.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					jPanelSpectrumView.setXScalePeriod(jCheckBoxXPeriod.isSelected());
				}
			});
		}
		return jCheckBoxXPeriod;
	}
	/**
	 * This method initializes jScrollPane2	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane2() {
		if (jScrollPane2 == null) {
			jScrollPane2 = new JScrollPane();
			jScrollPane2.setBounds(5, 548, 190, 86);
			jScrollPane2.setViewportView(getJTextAreaInputInfo());
		}
		return jScrollPane2;
	}
	/**
	 * This method initializes jCheckBoxSlideChannel	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxOffsetDisplay() {
		if (jCheckBoxOffsetDisplay == null) {
			jCheckBoxOffsetDisplay = new JCheckBox();
			jCheckBoxOffsetDisplay.setBounds(81, 83, 113, 22);
			jCheckBoxOffsetDisplay.setText("Offset Display");
			jCheckBoxOffsetDisplay.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					boolean b = jCheckBoxOffsetDisplay.isSelected();
					jPanelSpectrumView.setOffsetDisplay(b);
				}
			});
		}
		return jCheckBoxOffsetDisplay;
	}
	/**
	 * This method initializes jPanelBottomPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelBottomPanel() {
		if (jPanelBottomPanel == null) {
			jPanelBottomPanel = new JPanel();
			jPanelBottomPanel.setLayout(new BorderLayout());  // Generated
			jPanelBottomPanel.setPreferredSize(new Dimension(20,20));
			jPanelBottomPanel.add(getJButtonSnapShot(), BorderLayout.EAST);  // Generated
			jPanelBottomPanel.add(getJPanelWest(), BorderLayout.WEST);  // Generated
		}
		return jPanelBottomPanel;
	}
	/**
	 * This method initializes jPanelWest	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelWest() {
		if (jPanelWest == null) {
			jPanelWest = new JPanel();
			jPanelWest.setLayout(new BoxLayout(getJPanelWest(), BoxLayout.X_AXIS));  // Generated
			jPanelWest.add(getJCheckBoxAutoUpDate(), null);  // Generated
			jPanelWest.add(getJComboUpdatePeriod(), null);  // Generated
		}
		return jPanelWest;
	}
	/* (non-Javadoc)
	 * @see wcg.winchkgPanelInterface#addMenu(javax.swing.JMenuBar)
	 */
	@Override
	public void addMenu(JMenuBar mb) {
		
		JMenu menuFile = menuUtil.getMenu(mb,"File");
		if(menuFile == null){
			menuFile = menuUtil.setupMenu("File",KeyEvent.VK_F,"File",this);
			mb.add(menuFile);
		}

		// [File]-[----]
		menuFile.addSeparator();
		
		JMenuItem menuPrint = menuUtil.setupMenuItem("Print",KeyEvent.VK_P,"PrintSpectrum",this);
		menuPrint.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/printer.png")));

		menuPrint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PrinterJob job = PrinterJob.getPrinterJob();
				job.setPrintable(jPanelSpectrumView);
				job.setJobName("winchkg-spctrum");
				PageFormat page = new PageFormat();
				page.setOrientation(PageFormat.LANDSCAPE);
				Paper pp = new Paper();
				pp.setSize(210*72/25.4, 297*72/25.4);
				pp.setImageableArea(0.25*72, 0.25*72, 210*72/25.4 - 0.5*72,  297*72/25.4-0.5*72);

				page.setPaper(pp);
				job.pageDialog(page);
				if(job.printDialog()){
					try{
						job.print();
					}
					catch(Exception e){

					}
				}
			}

		});
		menuFile.add(menuPrint);

		// [ View ] ---------------------------------------------
		JMenu spectrumViewMenu = menuUtil.getMenu(mb, "View");
		if(spectrumViewMenu==null){
			spectrumViewMenu = menuUtil.setupMenu("View",KeyEvent.VK_V,"View",this);
			spectrumViewMenu.addMenuListener(this);
			mb.add(spectrumViewMenu);
		}
		// [View]-[----] 
		JMenu menuColor = menuUtil.setupMenu("Color",KeyEvent.VK_C,"Color",this);
		menuColor.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/rich_text_color.png")));
		spectrumViewMenu.add(menuColor);
		// [View]-[----] 

		ColorColorSpectrumWhite = new  JRadioButtonMenuItem("Color White background",true);
		ColorColorSpectrumWhite.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-rd-w.png")));
		//ColorColorSpectrumWhite.addItemListener(this);
		ColorColorSpectrumWhite.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorMode(SpectrumView.COLOR_COLOR_WHITE_BACK);
				getSpectrumView().repaintAll();
			}
		});

		menuColor.add(ColorColorSpectrumWhite);
		
		ColorColorSpectrumBlack = new  JRadioButtonMenuItem("Color Black background");
		ColorColorSpectrumBlack.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-rd-b.png")));
		//ColorColorSpectrumBlack.addItemListener(this);
		ColorColorSpectrumBlack.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				//getSpectrumView().setColorMode(getSpectrumView().COLOR_COLOR_BLACK_BACK);
				getSpectrumView().setColorMode(SpectrumView.COLOR_COLOR_BLACK_BACK);
				getSpectrumView().repaintAll();
			}
		});
		menuColor.add(ColorColorSpectrumBlack);
		
		ColorBWSpectrumWhite    = new  JRadioButtonMenuItem("Black&White White background");
		ColorBWSpectrumWhite.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-b-w.png")));
		//ColorBWSpectrumWhite.addItemListener(this);
		ColorBWSpectrumWhite.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorMode(SpectrumView.COLOR_BW_WHITE_BACK);
				getSpectrumView().repaintAll();		  
			}
		});
		menuColor.add(ColorBWSpectrumWhite);
		
		ColorBWSpectrumBlack    = new  JRadioButtonMenuItem("Black&White Black background");
		ColorBWSpectrumBlack.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/wave-w-b.png")));
		//ColorBWSpectrumBlack.addItemListener(this);
		ColorBWSpectrumBlack.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorMode(SpectrumView.COLOR_BW_BLACK_BACK);
				getSpectrumView().repaintAll();
			}
		});
		menuColor.add(ColorBWSpectrumBlack);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(ColorColorSpectrumWhite);
		bg.add(ColorColorSpectrumBlack);
		bg.add(ColorBWSpectrumWhite);
		bg.add(ColorBWSpectrumBlack);
		menuColor.addSeparator();
		
		ColorSpectrumMapByChannel    = new  JRadioButtonMenuItem("Color by Channel",true);
		//ColorSpectrumMapByChannel.addItemListener(this);
		ColorSpectrumMapByChannel.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorCalcMode(SpectrumView.COLOR_AUTO_BY_CHANNEL);
				getSpectrumView().repaintAll();	
			}
		});
		
		menuColor.add(ColorSpectrumMapByChannel);
		
		
		ColorSpectrumMapByStation    = new  JRadioButtonMenuItem("Color by Station");
		//ColorSpectrumMapByStation.addItemListener(this);
		ColorSpectrumMapByStation.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorCalcMode(SpectrumView.COLOR_AUTO_BY_STATION);
				getSpectrumView().repaintAll();	
			}
		});
		menuColor.add(ColorSpectrumMapByStation);
		
		
		ColorSpectrumMapByComponent    = new  JRadioButtonMenuItem("Color by Component");
		//ColorSpectrumMapByComponent.addItemListener(this);
		ColorSpectrumMapByComponent.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				getSpectrumView().setColorCalcMode(SpectrumView.COLOR_AUTO_BY_COMPONENT);
				getSpectrumView().repaintAll();	
			}
		});
		menuColor.add(ColorSpectrumMapByComponent);
		
		ButtonGroup bg2 = new ButtonGroup();
		bg2.add(ColorSpectrumMapByChannel);
		bg2.add(ColorSpectrumMapByStation);
		bg2.add(ColorSpectrumMapByComponent);
		
		
		// [ Data ] ---------------------------------------------
		JMenu spectrumDataMenu = menuUtil.getMenu(mb, "Data");
		if(spectrumDataMenu==null){
			spectrumDataMenu = menuUtil.setupMenu("Data",KeyEvent.VK_D,"Data",this);
			spectrumDataMenu.addMenuListener(this);
			mb.add(spectrumDataMenu);
		}
		spectrumDataMenu.addSeparator();
		// [View]-[----] 
		JMenuItem menuExportCSV = menuUtil.setupMenuItem("Export CSV",KeyEvent.VK_E,"ExportCSV",this);
		//menuColor.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/color.png")));
		menuExportCSV.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				getSpectrumView().exportCSV();
				
			}
		});
		spectrumDataMenu.add(menuExportCSV);
	
	}
	/* (non-Javadoc)
	 * @see javax.swing.event.MenuListener#menuCanceled(javax.swing.event.MenuEvent)
	 */
	@Override
	public void menuCanceled(MenuEvent e) {
		
	}
	/* (non-Javadoc)
	 * @see javax.swing.event.MenuListener#menuDeselected(javax.swing.event.MenuEvent)
	 */
	@Override
	public void menuDeselected(MenuEvent e) {
		
	}
	/* (non-Javadoc)
	 * @see javax.swing.event.MenuListener#menuSelected(javax.swing.event.MenuEvent)
	 */
	@Override
	public void menuSelected(MenuEvent e) {
		JMenu menu = (JMenu)e.getSource();
		String cmd = menu.getActionCommand();
		if(cmd.compareToIgnoreCase("VIEW")==0){
			// Spectrum Viewのカラーモードをフィードバック
			SpectrumView sv = getSpectrumView();
			if(sv!=null){
				int colormode = sv.getColorMode();
				switch(colormode){
				case SpectrumView.COLOR_BW_BLACK_BACK:
					if(ColorBWSpectrumBlack!=null){
						ColorBWSpectrumBlack.setSelected(true);
					}
					break;
				case SpectrumView.COLOR_BW_WHITE_BACK:
					if(ColorBWSpectrumWhite!=null){
						ColorBWSpectrumWhite.setSelected(true);
					}
					break;
				case SpectrumView.COLOR_COLOR_BLACK_BACK:
					if(ColorColorSpectrumBlack!=null){
						ColorColorSpectrumBlack.setSelected(true);
					}
					break;
				case SpectrumView.COLOR_COLOR_WHITE_BACK:
					if(ColorColorSpectrumWhite!=null) {
						ColorColorSpectrumWhite.setSelected(true);
					}
					break;
				}
			}
		}
		
	}
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
	}
	/* (non-Javadoc)
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		//----------------
		if(arg0.getSource()==ColorColorSpectrumWhite ){
			if(ColorColorSpectrumWhite.isSelected()){
//				getSpectrumView().setColorMode(getSpectrumView().COLOR_COLOR_WHITE_BACK);
//				getSpectrumView().repaintAll();		 
			}
			
		}
		else if(arg0.getSource()==ColorColorSpectrumBlack ){
			if(ColorColorSpectrumBlack.isSelected()){
//				getSpectrumView().setColorMode(getSpectrumView().COLOR_COLOR_BLACK_BACK);
//				getSpectrumView().repaintAll();		  
			}
		}
		else if(arg0.getSource()==ColorBWSpectrumWhite ){
			if(ColorBWSpectrumWhite.isSelected()){
//				getSpectrumView().setColorMode(getSpectrumView().COLOR_BW_WHITE_BACK);
//				getSpectrumView().repaintAll();		  
			}
		}
		else if(arg0.getSource()==ColorBWSpectrumBlack ){
//			if(ColorBWSpectrumBlack.isSelected()){
//				getSpectrumView().setColorMode(getSpectrumView().COLOR_BW_BLACK_BACK);
//				getSpectrumView().repaintAll();
//			}		  
		}
		else if(arg0.getSource()== ColorSpectrumMapByChannel){
//			getSpectrumView().setColorCalcMode(getSpectrumView().COLOR_AUTO_BY_CHANNEL);
//			getSpectrumView().repaintAll();	
		}
		else if(arg0.getSource()== ColorSpectrumMapByStation){
//			getSpectrumView().setColorCalcMode(getSpectrumView().COLOR_AUTO_BY_STATION);
//			getSpectrumView().repaintAll();	
		}
		else if(arg0.getSource()== ColorSpectrumMapByComponent){
//			getSpectrumView().setColorCalcMode(getSpectrumView().COLOR_AUTO_BY_COMPONENT);
//			getSpectrumView().repaintAll();	
		}

	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBorder(new TitledBorder(null, "Channels", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel_1.setBounds(0, 39, 200, 122);
			panel_1.setLayout(null);
			panel_1.add(getJScrollPane1());
			panel_1.add(getJButtonReCalc());
			panel_1.add(getJButtonAutoLevel());
			panel_1.add(getJCheckBoxOffsetDisplay());
		}
		return panel_1;
	}
}
